package com.yiren.dbaa.module.practice_set.basestation.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.basestation.bean.BaseStationBean;
import com.yiren.dbaa.module.practice_set.basestation.contract.CalibrationContract;
import com.yiren.dbaa.module.practice_set.basestation.presenter.CalibrationPresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/12 16:53
 * @description: 基站管理
 */
public class BaseStationManageActivity extends BaseActivity implements CalibrationContract.BaseManageView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_base_id)
    TextView tvBaseId;
    @BindView(R.id.et_base_name)
    EditText etBaseName;
    @BindView(R.id.tv_base_state)
    TextView tvBaseState;
    @BindView(R.id.tv_calibration)
    TextView tvCalibration;
    @BindView(R.id.tv_base_location)
    TextView tvBaseLocation;

    private BaseStationBean baseStationBean;
    private Intent intent;
    private CalibrationContract.Presenter calibrationPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_basestation);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        tvTitle.setText("基站管理");
        intent = getIntent();
        baseStationBean = (BaseStationBean) intent.getSerializableExtra("baseStation");
        etBaseName.setText(baseStationBean.getName());
        tvBaseId.setText(baseStationBean.getId());
        if (baseStationBean.getCalibration() == 1) {
            tvBaseState.setText("已标定");
            tvCalibration.setText("重新标定");
        } else {
            tvBaseState.setText("未标定");
            tvCalibration.setText("标定基站");
        }
        tvBaseLocation.setText(baseStationBean.getAddress());
        calibrationPresenter = new CalibrationPresenter(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_change_basebox, R.id.tv_change_name, R.id.tv_calibration, R.id.tv_change_loaction})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_change_basebox:  //更换基站盒
                calibrationPresenter.getNewBaseBoxId(this);
                break;
            case R.id.tv_change_name:     //修改名称
                String baseName = etBaseName.getText().toString().trim();
                if (StringUtils.isEmpty(baseName)) {
                    MessageBox.show(this, "基站名称不能为空！");
                    return;
                }
                if (StringUtils.equals(baseStationBean.getName(), baseName)) {
                    MessageBox.show(this, "基站名称未改变！");
                    return;
                }
                CommonDialog.showTwoBtn(this, "温馨提示", "您确定要将该基站名称修改为“" + baseName + "”吗？", "取消", "确认", (dialog, which) -> {
                    dialog.dismiss();
                    if (which != -2) {
                        calibrationPresenter.replaceBaseBoxName(baseStationBean.getId(), baseName, this);
                    }
                });
                break;
            case R.id.tv_calibration:
                if (baseStationBean.getCalibration() == 1) {
                    CommonDialog.showTwoBtn(this, "温馨提示", "当前“" + baseStationBean.getName() + "“基站已标定，是否需要重新标定？", "取消", "确定", (dialog, which) -> {
                        dialog.dismiss();
                        if (which != -2) {
                            Intent intent = new Intent(this, CalibrationActivity.class);
                            intent.putExtra("baseStation", baseStationBean);
                            startActivity(intent);
                        }
                    });
                }
                break;
            case R.id.tv_change_loaction:  //更换位置
                Intent intent = new Intent(this, CalibrationActivity.class);
                intent.putExtra("baseStation", baseStationBean);
                startActivity(intent);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setBaseId(String baseId) {
        CommonDialog.showTwoBtn(this, "操作确认", "确定将" + baseStationBean.getName() + "基站盒更换成ID为“" + baseId + "”基站盒吗？", "取消", "确定", (dialog, which) -> {
            dialog.dismiss();
            if (which != -2) {
                calibrationPresenter.replaceBaseBoxId(baseStationBean.getId(), baseId, this);
            }
        });
    }

    @Override
    public void updateBaseIdSuccess(String newBaseId) {
        MessageBox.show(this, "更换基站盒成功！");
        tvBaseId.setText(newBaseId);
    }

    @Override
    public void updateBaseNameSuccess(String baseName) {
        MessageBox.show(this, "修改成功！");
        baseStationBean.setName(baseName);
    }
}
